/*------------------------------------------------------------------------------*\
**   Copyright 2008-2009 Jonhnny Weslley                                        **
**                                                                              **
**   Licensed under the Apache License, Version 2.0 (the "License");            **
**   you may not use this file except in compliance with the License.           **
**   You may obtain a copy of the License at                                    **
**                                                                              **
**       http://www.apache.org/licenses/LICENSE-2.0                             **
**                                                                              **
**   Unless required by applicable law or agreed to in writing, software        **
**   distributed under the License is distributed on an "AS IS" BASIS,          **
**   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   **
**   See the License for the specific language governing permissions and        **
**   limitations under the License.                                             **
\*------------------------------------------------------------------------------*/

package org.scalashell.terminal

class Buffer {

  protected val buffer: StringBuilder = new StringBuilder

  def size = buffer.length

  def clear = buffer setLength 0

  def append(text: String) = {
    buffer append text
    this
  }

  def append(obj: Any) = {
    buffer append obj
    this
  }

  def attrib(code: Int) = {
    this
  }

  def attrib(text: String, code: Int) = {
    buffer append text
    this
  }

  override def toString = buffer.toString

}

class ANSIBuffer extends Buffer {

  import jline.ANSIBuffer.ANSICodes

  override def attrib(code: Int) = {
    buffer append (ANSICodes attrib code)
    this
  }

  override def attrib(text: String, code: Int) = {
    buffer
      .append(ANSICodes attrib code)
      .append(text)
      .append(ANSICodes attrib ANSI.Code.OFF)
    this
  }

}